(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:productcatItemList
     * @description
     * # productcatItemList 
     */
    angular.module('webApp')
        .directive('productcatItemList', function() {
            return {
                restrict: 'E',
                templateUrl: "views/directives.productcat.item.list.html",
                scope: {
                    callback: '&'
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'sku', 'helper', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, sku, helper) {
                    //1、事件 2、function 函数接受参数
                    $rootScope.$on('event:productcat-item-list', function(event, data) {
                        // console.log("data--->", data);
                        $scope.product_id = data.product_id;
                        $scope.pageData(1);
                    });

                    //请求数据
                    $scope.itemsTotal = 0;
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        var a = { page_no: pageNo ? pageNo : "1", page_size: $scope.pageSize };
                        a.query = { store_id: $rootScope.store_id };

                        // console.error("商品查询条件--->", a);
                        dataService.itemGet(a).then(function(itemList) {
                            $scope.itemsTotal = itemList.total_count;
                            $scope.itemslist = [];

                            if (itemList) {
                                async.mapSeries(itemList.items, function(item, call) {
                                    var item_id = item.item_id;
                                    var data = {
                                        "content": { "item_id": item_id, "product_id": $scope.product_id }
                                    };
                                    dataService.fxproductitemGet(data).then(function(result) {
                                        if (result.errmsg == "ok") {
                                            item.proportion = result.itemData.proportion;
                                            item.itemDelete = true;
                                            $scope.itemslist.push(item);
                                        } else {
                                            item.proportion = 0;
                                            item.itemDelete = false;
                                            $scope.itemslist.push(item);
                                        }
                                    });
                                    // console.log("dss111111111111111", $scope.itemslist);
                                    call(null, null);
                                }, function(e, r) {
                                    // console.log("sdlkfjsdlfksjdlfkjowr230r932", $scope.itemslist);
                                })
                            }
                        });
                    }

                    // 产品线商品添加
                    $scope.addProductItem = function(item) {

                        if (!item.proportion) {
                            helper.myAlert("error：返点比例不能为空");
                            return false;
                        }

                        if ($scope.product_id) {
                            item.product_id = $scope.product_id;
                        }

                        var checked_item = {
                            "product_id": item.product_id,
                            "item_id": item.item_id,
                            "proportion": item.proportion
                        };

                        var data = {
                            "content": checked_item
                        };
                        dataService.fxproductItemAdd(data).then(function(result) {
                            if (result.errmsg == "ok") {
                                item.itemDelete = true;
                                $scope.callback({ "fenxiao_item_num": 1 });
                            } else {
                                helper.myAlert("error：" + result.errmsg);
                                return false;
                            }
                        });
                    }

                    //产品线商品退出
                    $scope.productitemDelete = function(product_id, item_id, item) {
                        var data = {
                            "content": { "product_id": product_id, "item_id": item_id }
                        };

                        dataService.fxproductItemDelete(data).then(function(result) {
                            if (result.errmsg == "ok") {
                                item.itemDelete = false;
                            } else {
                                helper.myAlert("error：" + result.errmsg);
                                return false;
                            }
                        });
                    }
                }]
            }
        })
})();